﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using CommonLibraries.DeviceLibrary.Helpers;
using System.Collections.Generic;
using CommonLibraries.DeviceLibrary;

namespace demo
{
    public partial class ViewPhotosForm : BaseForm
    {
        protected int m_IdPosition;
        public int IdPosition
        {
            set { m_IdPosition = value; }
        }
        public ViewPhotosForm()
        {
            InitializeComponent();
            this.buttonFocus.Focus();
        }
        protected List<string> m_PathImages = null;

        private void transparentLabelExit_Click(object sender, EventArgs e)
        {
            ViewPhotoFormClose();
        }
        protected void ViewPhotoFormClose()
        {
            this.pictureBoxPreview.Image = null;
            this.panelPreviewPhotos.Controls.Clear();
            m_GPS.HasSolutionChanged -= new EventHandler(m_GPS_HasSolutionChanged);
            this.Close();
        }

        void ViewPhotosForm_Load(object sender, System.EventArgs e)
        {
            m_PathImages = new List<string>(); 
            LoadPhotos();
            LoadPhotosInPanel();
        }

        private void LoadPhotos()
        {
            m_PathImages = ImageHelper.FindImageByPosition(m_IdPosition.ToString(), Constants.K_ImagesFolder);
        }
        protected bool LoadPhotosInPanel()
        {
            try
            {
                int _X = 3;
                int _Y = 3;

                //Clear the panel
                panelPreviewPhotos.Controls.Clear();
                for (Int32 i = 0; i < m_PathImages.Count; i++)
                {
                    //define X Distance between photos
                    int _Distance = 51 * i;

                    //Attach photo to the picturebox
                    PictureBox PictureBoxPreview;
                    PictureBoxPreview = new PictureBox();
                    PictureBoxPreview.Width = 41;
                    PictureBoxPreview.Height = 42;
                    PictureBoxPreview.SizeMode = PictureBoxSizeMode.StretchImage;

                    PictureBoxPreview.Image =ImageHelper.GetImage(m_PathImages[i]);
                    PictureBoxPreview.Location = new System.Drawing.Point(_X + _Distance, _Y);
                    PictureBoxPreview.Click += new EventHandler(PictureBoxPreview_Click);
                    panelPreviewPhotos.Controls.Add(PictureBoxPreview);
                }
                return true;
            }
            catch (Exception e)
            {
                ErrorManager.CreateDebug("LoadPhotosInPanel->" + e.Message);
                return false;
            }
        }

        void PictureBoxPreview_Click(object sender, EventArgs e)
        {
            PictureBox temp = (PictureBox)sender;
            pictureBoxPreview.Image = temp.Image;
            
        }

        public override void buttonFocus_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            switch (e.KeyValue)
            {
                case 27:
                    ViewPhotoFormClose();
                    break;
            }
        }


    }
}